﻿var bShowHistoryPres = false;
var timer = null;
var delay = 5000;

function showHideHistoryPres() {
    bShowHistoryPres = ('inline' == $get(g_str_IDFor_HistoryPresentation).style.display);
    if (bShowHistoryPres) {
        $get(g_str_IDFor_HistoryPresentation).style.display = 'none';
        $get(g_str_IDFor_HistoryHeaderPresentation).className = 'span1';
        $get(g_str_IDFor_HistoryActionPresentation).innerText = 'arata';
        bShowHistoryPres = false;
    }
    else {
        $get(g_str_IDFor_HistoryPresentation).style.display = 'inline';
        $get(g_str_IDFor_HistoryHeaderPresentation).className = 'menu_th';
        $get(g_str_IDFor_HistoryActionPresentation).innerText = 'ascunde';
        bShowHistoryPres = true;
    }
}

function getItemRow(parafa, contract, specialitate) {
    var parafaObj = $get(g_str_txtParafa);
    var contractObj = $get(g_str_txtContract);
    var specialitateObj = $get(g_str_txtSpecialitate);
    parafaObj.value = parafa;
    contractObj.value = contract;
    specialitateObj.value = specialitate;
    $find(g_str_mdlPopup).hide();
    //CallServer(null, "");
    //PageMethods.SendMessage(parafa, ReceiveServerData, errorCallback);
}

function getItemRowSpec(specialitate) {
    var specialitateObj = $get(g_str_txtSpecialitate);
    specialitateObj.value = specialitate;
    $find(g_str_mdlPopupSpec).hide();
}

function ReceiveServerData(retValue)
{
    
}

function errorCallback()
{

}

function StartTheTimer() {
    timer = window.setInterval("getTime()", delay);
}

function hideShowModul() {
    $get(g_str_trPresData).style.display = 'none';

}

//////////////////////////////////////////////////

var lstInterval = new Array();
var countInterval = 0;
var nInterval;
var tempRefID;

function Interval() {
    var mID = null;
    var UID = null;
    var RefID = null;
    var minValue = null;
    var maxValue = null;
    var sex = null;
    var ageLo = null;
    var ageHi = null;
    var isLoaded = false;
    var toDelete = false;
    var bModificat = false;
}

function saveInterval(id) {
    PageMethods.insertNormalValues(lstInterval, id, onGetdataSuccess, onGetdataFail);
}

function displayIntervals(id) {
    tempRefID = id;
    PageMethods.getNormalValues(id, onGetdataSuccess, onGetdataFail);
}

function onGetdataSuccess(response) {
    var tblSelected = $get(g_str_tblNormalValues);
    var rows = tblSelected.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
    lstInterval = [];

    for (var i = 0; i < response.length; ++i) 
    {
        var tempInterval = response[i].split(";");
        countInterval++;
        nInterval = new Interval();
        nInterval.mID = countInterval;
        nInterval.UID = tempInterval[0];
        nInterval.RefID = tempInterval[1];
        if (tempInterval[2] == "True")
            nInterval.sex = 'M';
        else
            nInterval.sex = 'F';
        nInterval.ageLo = tempInterval[3];
        nInterval.ageHi = tempInterval[4];
        nInterval.minValue = tempInterval[5];
        nInterval.maxValue = tempInterval[6];
        nInterval.isLoaded = true;
        nInterval.toDelete = false;
        nInterval.bModificat = false;
        lstInterval[lstInterval.length] = nInterval;
    }

    for (var i = 0; i < lstInterval.length; i++) {
        var nRow = tblSelected.insertRow(rows.length + 1);
        nRow.value = lstInterval[i].mID;
        var nCell = nRow.insertCell(0);
        nCell.align = "center";
        nCell.className = "tdn";
        nCell.innerHTML = "<input type=button value='x' style='margin-top: 1px; color: #442200;font-size:6pt;font-family:Arial' title='Sterge' onclick='CancelNormalValue(" + lstInterval[i].mID + ")'>";
        nCell = nRow.insertCell(1);
        nCell.className = "tdn";
        var strT = "";
        strT = "<select ID='SEX" + lstInterval[i].mID + "' onchange='ModifyNormalValue(" + lstInterval[i].mID + ")'> <option value='M'";
        if (lstInterval[i].sex + "" == "M") {
            strT = strT + " SELECTED ";
        }
        strT = strT + ">Masculin</option><option value='F'";
        if (lstInterval[i].sex + "" == "F") {
            strT = strT + " SELECTED ";
        }
        strT = strT + ">Feminin</option></select>";
        nCell.innerHTML = strT;
        nCell = nRow.insertCell(2);
        nCell.className = "tdn";
        nCell.align = "center";
        nCell.innerHTML = "<input type=text value='" + lstInterval[i].ageLo + "' id='ageMin" + lstInterval[i].mID + "' maxlength=3 size=3 onchange='ModifyNormalValue(" + lstInterval[i].mID + ")'>";
        nCell = nRow.insertCell(3);
        nCell.className = "tdn";
        nCell.align = "center";
        nCell.innerHTML = "<input type=text value='" + lstInterval[i].ageHi + "' id='ageMax" + lstInterval[i].mID + "' maxlength=3 size=3 onchange='ModifyNormalValue(" + lstInterval[i].mID + ")'>";
        nCell = nRow.insertCell(4);
        nCell.className = "tdn";
        nCell.align = "center";
        nCell.innerHTML = "<input type=text value='" + lstInterval[i].minValue + "' id='minValue" + lstInterval[i].mID + "' maxlength=20 onchange='ModifyNormalValue(" + lstInterval[i].mID + ")'>";
        nCell = nRow.insertCell(5);
        nCell.className = "tdn";
        nCell.align = "center";
        nCell.innerHTML = "<input type=text value='" + lstInterval[i].maxValue + "' id='maxValue" + lstInterval[i].mID + "' maxlength=20 onchange='ModifyNormalValue(" + lstInterval[i].mID + ")'>";
    }
}

function CancelNormalValue(mID) {
    var tblSelected = $get(g_str_tblNormalValues);
    var elm, i;
    for (i = 0; i < lstInterval.length; i++) {
        elm = lstInterval[i];
        if (elm.mID == mID) {
            elm.toDelete = true;
            if (elm.isLoaded == true) {
                elm.bModificat = true;
            } else {
                elm.bModificat = false;
            }
            break;
        }
    }

    var rows = tblSelected.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
    for (i = 0; i < rows.length; i++) {
        if (rows[i].value == mID) {
            var tblSelected = tblSelected;
            tblSelected.deleteRow(i + 1);
            break;
        }
    }
}

function ModifyNormalValue(mID){
	var elm,i;
	for (i=0;i<lstInterval.length;i++){
		elm=lstInterval[i];
		if (elm.mID==mID){
			elm.bModificat=true;
			field = document.getElementById("SEX" + mID);
			elm.sex = field.value;
			field=document.getElementById("ageMin"+mID);
			elm.ageLo=field.value;
			field=document.getElementById("ageMax"+mID);
			elm.ageHi=field.value;
			field=document.getElementById("minValue"+mID);
			elm.minValue=field.value;
			field=document.getElementById("maxValue"+mID);
			elm.maxValue=field.value;
		}
	}
}

function addNewInterval() {
    var tblSelected = $get(g_str_tblNormalValues);
    var rows = tblSelected.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
    countInterval++;
    var nInterval = new Interval();
    var nRow = tblSelected.insertRow(rows.length + 1);
    nRow.value = countInterval;
    var nCell = nRow.insertCell(0);
    nCell.align = "center";
    nCell.style.width = "12%";
    nCell.innerHTML = "<input type=button value='x' style='margin-top: 1px; color: #442200;font-size:6pt;font-family:Arial' title='Sterge' onclick='CancelNormalValue(" + countInterval + ")'>";
    nCell = nRow.insertCell(1);
    nCell.align = "center";
    nCell.style.width = "12%";
    nCell.innerHTML = "<select ID='SEX" + countInterval + "' onchange='ModifyNormalValue(" + countInterval + ")'> <option value='M'>Masculin</option><option value='F'>Feminin</option></select>";
    nCell = nRow.insertCell(2);
    nCell.align = "center";
    nCell.style.width = "14%";
    nCell.innerHTML = "<input type=text value='' id='ageMin" + countInterval + "' maxlength=3 size=3 onchange='ModifyNormalValue(" + countInterval + ")'>";
    nCell = nRow.insertCell(3);
    nCell.align = "center";
    nCell.style.width = "14%";
    nCell.innerHTML = "<input type=text value='' id='ageMax" + countInterval + "' maxlength=3 size=3 onchange='ModifyNormalValue(" + countInterval + ")'>";
    nCell = nRow.insertCell(4);
    nCell.align = "center";
    nCell.style.width = "25%";
    nCell.innerHTML = "<input type=text value='' id='minValue" + countInterval + "' maxlength=20 onchange='ModifyNormalValue(" + countInterval + ")'>";
    nCell = nRow.insertCell(5);
    nCell.align = "center";
    nCell.style.width = "25%";
    nCell.innerHTML = "<input type=text value='' id='maxValue" + countInterval + "' maxlength=20 onchange='ModifyNormalValue(" + countInterval + ")'>";
    nInterval.mID = countInterval;
    nInterval.UID = null;
    nInterval.RefID = tempRefID;
    nInterval.sex = "M";
    nInterval.ageLo = "";
    nInterval.ageHi = "";
    nInterval.minValue = "";
    nInterval.maxValue = "";
    nInterval.toDelete = false;
    nInterval.isLoaded = false;
    nInterval.bModificat = false;
    lstInterval[lstInterval.length] = nInterval;
}

function onGetdataFail(result, userContext, methodName) {
    var error = "Method: " + methodName + "\nTimeout: " + result.get_timedOut() + "\nMessage: " + result.get_message();
    alert(error);
}

////////////////////////////////////////////////////////////
var countType = 0;
var tempRefType;
var typeRef;
var lstTypeRef = new Array();

function TypeRef() {
    var RefResID = null;
    var typeName = null;
}

function resetArray() {
    lstTypeRef = [];
    lstInterval = [];
}

function displayType(id) {
    tempRefType = id;
    PageMethods.getType(id, onGetdataSuccessType, onGetdataFail);
}

function onGetdataSuccessType(response) 
{
    var oTbl = $get(g_str_tblTypes);
    lstTypeRef = [];
    for (var i = 0; i < response.length; i++) {
        var tempType = response[i].split(";");
        countType++;
        typeRef = new TypeRef();
        typeRef.RefResID = tempType[1];
        typeRef.typeName = tempType[0];
        var oTr = oTbl.insertRow();
        oTr.id = "trType" + countType;
        var oTd = oTr.insertCell();
        oTd.className = "tdn";
        oTd.colSpan = 2;
        oTd.innerHTML = "<input type='hidden' name='ResTypes' id='ResTypes" + i + "' value='" + typeRef.RefResID + "'>" + typeRef.typeName;
        var oTd1 = oTr.insertCell();
        oTd1.innerHTML = "<img src='../../Images/Hipocrate/del.gif' border='0' alt='Click pentru a sterge' WIDTH='16' HEIGHT='16' style='cursor: hand' onclick='removeType(" + oTr.id + "," + typeRef.RefResID + ")''>"
        lstTypeRef[lstTypeRef.length] = typeRef;
    }
}

function removeType(mID, resID) 
{
    var oTbl = $get(g_str_tblTypes);
    for (i = 0; i < oTbl.rows.length; i++) 
    {
        if (oTbl.rows[i].id == mID.id) 
        {
            oTbl.deleteRow(i);
            for (i = 0; i < lstTypeRef.length; i++) {
                tmp = lstTypeRef[i];
                if (tmp.RefResID == resID) {
                    lstTypeRef.splice(i, 1);
                }
            }
            break;
        }
    }
}

function addType() {
    var oTbl = $get(g_str_tblTypes);
    var oDrp = $get(g_str_drpTypesRefs);
    var SelectedVal = oDrp.options[oDrp.selectedIndex].text;

    typeRef = new TypeRef();
    typeRef.RefResID = oDrp.options[oDrp.selectedIndex].value;
    typeRef.typeName = SelectedVal;

    for (i = 0; i < lstTypeRef.length; i++) {
        tmp = lstTypeRef[i];
        if (tmp.RefResID == typeRef.RefResID) {
            return;
        }
    }

    var oTr = oTbl.insertRow();
    countType++;
    oTr.id = "trType" + countType;
    var oTd = oTr.insertCell();
    oTd.className = "tdn";
    oTd.colSpan = 2;
    oTd.innerHTML = "<input type='hidden' name='ResTypes' id='ResTypes" + (oTbl.rows.length - 1) + "' value='" + SelectedVal + "'>" + SelectedVal;
    var oTd1 = oTr.insertCell();
    oTd1.innerHTML = "<img src='../../Images/Hipocrate/del.gif' border='0' alt='Click pentru a sterge' WIDTH='16' HEIGHT='16' style='cursor: hand' onclick='removeType(" + oTr.id + "," + typeRef.RefResID + ")''>"
    lstTypeRef[lstTypeRef.length] = typeRef;
}

function saveType(id) {
    PageMethods.insertTypeValues(lstTypeRef, id, onGetdataSuccessType, onGetdataFail);
}

function IsNumber(number) {
    if ((number >= 0) || (number < 0)) return true;
    else return false;
}

function validateCNP(strCNP) {
    var S1, A2, A3, L4, L5, Z6, Z7, J8, J9, N10, N11, N12, C13, sum, C;
    if (strCNP.length == 13 && IsNumber(strCNP)) {
        S1 = parseInt(strCNP.substr(0, 1));
        A2 = parseInt(strCNP.substr(1, 1));
        A3 = parseInt(strCNP.substr(2, 1));
        L4 = parseInt(strCNP.substr(3, 1));
        L5 = parseInt(strCNP.substr(4, 1));
        Z6 = parseInt(strCNP.substr(5, 1));
        Z7 = parseInt(strCNP.substr(6, 1));
        J8 = parseInt(strCNP.substr(7, 1));
        J9 = parseInt(strCNP.substr(8, 1));
        N10 = parseInt(strCNP.substr(9, 1));
        N11 = parseInt(strCNP.substr(10, 1));
        N12 = parseInt(strCNP.substr(11, 1));
        C13 = parseInt(strCNP.substr(12, 1));
        sum = (S1 * 2) + (A2 * 7) + (A3 * 9) + (L4 * 1) + (L5 * 4) + (Z6 * 6) + (Z7 * 3) + (J8 * 5) + (J9 * 8) + (N10 * 2) + (N11 * 7) + (N12 * 9);
        C = sum % 11;

        if (C < 10 && C != C13)
            return false;
    }
    else
        return false;

    return true;
}

function getBirthDateFromCNP(strCNP) {
    var yearPrefix = (parseInt(strCNP.substr(1, 2)) < 20 ? "20" : "19");
    var sDate = strCNP.substr(5, 2) + "/" + strCNP.substr(3, 2) + "/" + yearPrefix + strCNP.substr(1, 2);
    return sDate;
}

function getSexFromCNP(strCNP) {
    var sex = strCNP.substr(0, 1);
    if (parseInt(sex, 10) == 1 || parseInt(sex, 10) == 5) {
        return "M";
    }
    else if (parseInt(sex, 10) == 2 || parseInt(sex, 10) == 6) {
        return "F";
    }
    else
        return "I"; 		/// invalid;
}

function cl_validate() {
    var strCNP = $get(g_str_txtInvCNP);
    var strSex = $get(g_str_drpInvSex);
    var strBirthDate = $get(g_str_txtInvNascut);
    strSex.selectedIndex = 0;
    strBirthDate.value = "";
    
    if (!isNaN(strCNP.value)) {
        if (validateCNP(strCNP.value)) 
        {
            if (strBirthDate.value == "") 
            {
                strBirthDate.value = getBirthDateFromCNP(strCNP.value);
            }
            if (strSex.selectedIndex == 0) 
            {
                var sSex = getSexFromCNP(strCNP.value);
                if (sSex == "I") 
                {
                    alert("Campul CNP este invalid!");
                    strCNP.focus();
                    return false;
                }
                else if (sSex == "M") 
                {
                    strSex.selectedIndex = 1;
                }
                else if (sSex == "F") 
                {
                    strSex.selectedIndex = "2";
                }
            }
        }
        else
            alert("Campul CNP nu poate fi validat!");
    }
    else
        alert("Campul CNP nu este numar!");
}

function get_W() {
    var w = 0;
    //IE
    if (!window.innerWidth) {
        //strict mode
        if (!(document.documentElement.clientWidth == 0)) {
            w = document.documentElement.clientWidth;
        }
        //quirks mode
        else {
            w = document.body.clientWidth;
        }
    }
    //w3c
    else {
        w = window.innerWidth;
    }
    return w;
}

function get_H() {
    var h = 0;
    //IE
    if (!window.innerWidth) {
        //strict mode
        if (!(document.documentElement.clientWidth == 0)) {
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else {
            h = document.body.clientHeight;
        }
    }
    //w3c
    else {
        h = window.innerHeight;
    }
    return h;
}


